iT邦幫忙

2022 iThome 鐵人賽

DAY 28
0
Modern Web

clojure 刷刷鍋系列 第 28

Clojure 肉片 -第 28 塊

  • 分享至 

  • xImage
  •  

【今日湯底】

Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.

Examples:

spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw"
spinWords( "This is a test") => returns "This is a test"
spinWords( "This is another test" )=> returns "This is rehtona test"

(必須通過以下測試)

(ns clojure.spin-words-test
  (:require [clojure.test :refer :all]
            [clojure.spin-words :refer :all]))
            
(deftest one-word-test
  (is (= (spin-words "Welcome") "emocleW"))) 

【我的答案】

(ns clojure.spin-words
  (:require [clojure.string :as str]))

(defn spin-words [strng]
  (->> (str/split strng #" ")
    (map #(if (>= (count %) 5) (apply str (reverse %)) %))
    (str/join " "))
  )

【其他人的答案】

(ns clojure.spin-words
  (:require [clojure.string :as string]))

(defn spin-words [s]
  (string/replace s #"\w{5,}" string/reverse))

上一篇
Clojure 肉片 -第 27 塊
下一篇
Clojure 肉片 -第 29 塊
系列文
clojure 刷刷鍋30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言